//
//  BookmarkAccess.m
//  LabanBrowser
//
//  Created by -Xh- on 3/7/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "BookmarkAccess.h"

#import "StaticVariables.h"
@implementation BookmarkAccess
-(NSMutableArray *)getRecordsFromSelectQuery:(NSString *)_query{
    NSMutableArray* bookmarkArray=[[NSMutableArray alloc]init];
    [database open];
    FMResultSet* queryResult=[database executeQuery:_query];
    while ([queryResult next]) {
        BookmarkModel* currentBookmark=[[BookmarkModel alloc]init];
        currentBookmark.title=[queryResult stringForColumn:@"Title"];
        currentBookmark.url=[queryResult stringForColumn:@"Url"];
        currentBookmark.favIcon=[queryResult dataForColumn:@"FavIcon"];
        currentBookmark.dateAdded=[queryResult dateForColumn:@"DateAdded"];
        currentBookmark.folderID=[queryResult stringForColumn:@"FolderID"];
        currentBookmark.dateLastModified=[queryResult dateForColumn:@"DateLastModified"];
        [bookmarkArray addObject:currentBookmark];
    }
    [database close];
    return bookmarkArray;
}

-(NSMutableArray *)getAllRecords{
    NSString* queryString=[NSString stringWithFormat:@"SELECT * FROM BOOKMARK ORDER BY BOOKMARK.DateAdded DESC"];
    return [self getRecordsFromSelectQuery:queryString];
}

-(BOOL)insertFromHistory:(HistoryModel *)history{

    //update on list data that we got before

    //insert into bookmark
    BookmarkModel* bookmark=[[BookmarkModel alloc]init];
    bookmark.title=history.title;
    bookmark.url = history.url;
    bookmark.favIcon = history.favIcon;
    bookmark.dateAdded=[NSDate date];
    bookmark.dateLastModified=bookmark.dateAdded;
    return [self insert:bookmark];
}

-(BOOL)insert:(BaseModel *)_object{
    BOOL success;
    BookmarkModel* insertingBookmark=(BookmarkModel*)_object;
    NSString* modifiedTitle=[insertingBookmark.title stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
    BookmarkModel* bookmarkOnDB = [self findByURL:insertingBookmark.url];
    
    [database open];
    if(bookmarkOnDB != nil){
        NSString* updateQuery=[NSString stringWithFormat:@"UPDATE BOOKMARK SET DateLastModified='%@' AND Title = '%@' WHERE Url='%@'",insertingBookmark.dateLastModified,insertingBookmark.title, bookmarkOnDB.url];
        success = [database executeUpdate:updateQuery];
    }else{
        NSString* insertQuery=[NSString stringWithFormat:@"INSERT INTO BOOKMARK (Title,FolderID,DateAdded,DateLastModified,Url,FavIcon) VALUES ('%@','%@','%@','%@','%@','%@')",modifiedTitle,insertingBookmark.folderID,insertingBookmark.dateAdded,insertingBookmark.dateLastModified,insertingBookmark.url,insertingBookmark.favIcon];
        success = [database executeUpdate:insertQuery];

    }
    
        
    
    [database close];
    return success;
}
-(BOOL)updateObjectAtURL:(NSString *)_url withNewObject:(BaseModel *)_newObject{
    BookmarkModel* newBookmark=(BookmarkModel*)_newObject;
    [database open];
    NSString* updateQuery=[NSString stringWithFormat:@"UPDATE BOOKMARK SET Title='%@', DateLastModified='%@' WHERE Url='%@'",newBookmark.title,[NSDate date],_url];
    BOOL success = [database executeUpdate:updateQuery];
    [database close];
    return success;
}


-(BookmarkModel *)findByURL:(NSString *)_url{
    //the result to return
    BookmarkModel* currentBookmark=[[BookmarkModel alloc]init];
    [database open];
    NSString* queryString=[NSString stringWithFormat:@"SELECT * FROM BOOKMARK WHERE Url='%@'",_url];
    FMResultSet* queryResult=[database executeQuery:queryString];
    if([queryResult next]){

        currentBookmark.title=[queryResult stringForColumn:@"Title"];
        currentBookmark.url=[queryResult stringForColumn:@"Url"];
        currentBookmark.favIcon=[queryResult dataForColumn:@"FavIcon"];
        currentBookmark.dateAdded=[queryResult dateForColumn:@"DateAdded"];
        currentBookmark.dateLastModified=[queryResult dateForColumn:@"DateLastModified"];
        currentBookmark.folderID=[queryResult stringForColumn:@"FolderID"];
        
    }else{
        currentBookmark=nil;
    }
    [database close];
    return currentBookmark;

}
-(BOOL)deleteObjectAtURL:(NSString *)_url{
    BOOL success;
    [database open];
    NSString* deleteQuery=[NSString stringWithFormat:@"DELETE FROM BOOKMARK WHERE Url='%@'",_url];
    success = [database executeUpdate:deleteQuery];
    [database close];
    return success;
}


@end
